package cn.qiao.demo.config;


import java.io.*;

public class MyClassLoad extends ClassLoader {

    @Override
    public Class<?> loadClass(String name) throws ClassNotFoundException {
        if ("cn.qiao.domain.Test".equals(name)){
            System.out.println("---------------");
        }
        Class<?> loadedClass = findLoadedClass(name);
        if (loadedClass == null){
            return findClass(name);
//            return super.loadClass(name);
        }else {
            return loadedClass;
        }
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        if ("cn.qiao.domain.Test".equals(name)){
            System.out.println("---------------");
        }
        try {
            FileInputStream stream = new FileInputStream("G:\\java\\exercise\\project\\SpringBootDemo\\target\\classes\\cn\\qiao\\demo\\domain\\Person1.class");

            byte[] bytes = new byte[1024];
            int read;
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            while (-1 != stream.read(bytes)){
                outputStream.write(bytes,0,bytes.length);
            }
            byte[] bytes1 = outputStream.toByteArray();
            outputStream.close();
            Class<?> aClass = defineClass(name, bytes, 0, bytes1.length);
            return aClass;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
